//
//  GLActionSheet.m
//  GLLibDemo
//
//  Created by huanggulong on 2017/11/1.
//  Copyright © 2017年 历山大亚. All rights reserved.
//

#import "GLActionSheet.h"
#import "NSString+GL.h"
#import "UIView+GL.h"
@interface GLActionSheet ()

@property(nonatomic , weak)UIView * contentView;

@property(nonatomic , strong)NSMutableArray * titleButtons;

//@property(nonatomic , <##>)<#type#> * <#name#>;

@property(nonatomic , weak)UIButton * cancelButton;

@end

@implementation GLActionSheet

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles{
    self = [super init];
    if (self) {
        _buttonTitles = buttonTitles;
        [self setUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _normalColor = [UIColor colorWithRed:33/255.0 green:33/255.0 blue:33/255.0 alpha:1];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
        UIView *contentView = [[UIView alloc] init];
        contentView.backgroundColor = @"#ececec".toColor;
        [self addSubview:contentView];
        _contentView = contentView;
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchSelf:)]];
        
        
    }
    return self;
}

- (void)removeFromSuperview{
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.frame = CGRectMake(0, self.height, _contentView.width, _contentView.height);
        self->_contentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    _contentView.frame = CGRectMake(0, self.height, self.width, _titleButtons.count * 50 + 58);
    _cancelButton.frame = CGRectMake(0, _contentView.height - 50, _contentView.width, 50);
    for (int i = 0; i < self.titleButtons.count; i ++) {
        UIButton *button = [self.titleButtons objectAtIndex:i];
        button.frame = CGRectMake(0, i * 50, _contentView.width, 49);
    }
}


#pragma mark - click
- (IBAction)cancelClick:(id)sender{
    [self removeFromSuperview];
}

- (IBAction)buttonClick:(id)sender{
    if (sender == nil) {
        return;
    }
    NSInteger index = [self.titleButtons indexOfObject:sender];
    if (self.didSelectedBlock) {
        self.didSelectedBlock(index);
    }
    [super removeFromSuperview];
}

- (void)touchSelf:(UIGestureRecognizer *)gesture{
    CGPoint point = [gesture locationInView:self];
    if (!CGRectContainsPoint(_contentView.frame, point)) {
        [self removeFromSuperview];
    }
}

#pragma mark - private

- (void)setUI{
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _titleButtons = [NSMutableArray array];
    UIColor *color = [[UIColor whiteColor] colorWithAlphaComponent:1];
    for (int i = 0; i < self.buttonTitles.count; i ++) {
        
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        [button setBackgroundColor:color];
        [button setTitle:_buttonTitles[i] forState:(UIControlStateNormal)];
        [button setTitleColor:_normalColor forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.contentView addSubview:button];
        [_titleButtons addObject:button];
    }
    
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [cancelButton setBackgroundColor:color];
    [cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
    [cancelButton setTitleColor:_normalColor forState:(UIControlStateNormal)];
    [cancelButton addTarget:self action:@selector(cancelClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.contentView addSubview:cancelButton];
    _cancelButton = cancelButton;
}

- (void)setNormalColor:(UIColor *)normalColor{
    _normalColor = normalColor;
    for (UIButton * button in _titleButtons) {
        [button setTitleColor:_normalColor forState:(UIControlStateNormal)];
    }
}

#pragma mark - public
- (void)showInView:(UIView *)view{
    [view addSubview:self];
    self.frame = view.bounds;
    
    [UIView animateWithDuration:0.25 delay:0.1 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.16];
        self.contentView.frame = CGRectMake(0, self.height - _contentView.height, _contentView.width, _contentView.height);
    } completion:^(BOOL finished) {
        
    }];
}


@end
